package minijvm.cmd;

import minijvm.clz.ClassFile;
import minijvm.constant.FieldRefInfo;
import minijvm.engine.ExecutionResult;
import minijvm.engine.JavaObject;
import minijvm.engine.StackFrame;

/**
 * @author ChangLiang
 * @date 2020/4/30
 */
public class GetFieldCmd extends TwoOperandCmd {

    public GetFieldCmd(ClassFile clzFile, String opCode) {
        super(clzFile, opCode);
    }

    @Override
    public void execute(StackFrame frame, ExecutionResult result) {
        FieldRefInfo fieldRefInfo = (FieldRefInfo) this.getConstantInfo(this.getIndex());
        String fieldName = fieldRefInfo.getFieldName();
        JavaObject javaObject = frame.getOperandStack().pop();
        JavaObject fieldValue = javaObject.getFieldValue(fieldName);
        frame.getOperandStack().push(fieldValue);
    }
}
